package com.wu.store.config;

import com.wu.store.config.resolver.AdminUserArgumentResolver;
import com.wu.store.config.resolver.CurrentUserArgumentsResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {
	private String dir = System.getProperty("user.dir") + "/goodsImg/";

	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
		argumentResolvers.add(currentUserArgumentsResolver());
		argumentResolvers.add(adminUserArgumentResolver());
	}

	@Bean
	public CurrentUserArgumentsResolver currentUserArgumentsResolver() {
		return new CurrentUserArgumentsResolver();
	}

	@Bean
	public AdminUserArgumentResolver adminUserArgumentResolver() {
		return new AdminUserArgumentResolver();
	}

	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		dir = "file:" + dir;
		CacheControl cacheControl = CacheControl.noCache();
		registry.addResourceHandler("/goodsImg/**").addResourceLocations(dir).setCacheControl(cacheControl);
	}
}
